﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using RTSoft.Modules;
using System.Data;

namespace RTSoft.SoureCode.Control.LoaiHang
{
    class loaihangctrl
    {
        DataProvider provider = new DataProvider();
        SqlConnection con = null;
        SqlCommand cmd = new SqlCommand();            
        String strSQL = "";
        	
	    public void getData(ref String errConnect, ref String errSQL, ref DataTable tbl){
		    con = provider.Connection(ref errConnect);
		    try
		    {
			    tbl = new DataTable();
                strSQL = "select * from LOAI_HANG";
			    tbl = provider.LayDanhSach(strSQL);
		    }
		    catch (Exception ex) 
		    {
			    errSQL = ex.Message;				
		    }
		    finally 
		    {
			    provider.Disconnect();
		    }
	    }

       
        public int getRowID(ref String errConnect, ref String errSQL, String code)
        {
            int result = 0;
            provider.Connection(ref errConnect);
            try
            {
                strSQL = "select ID_LOAI from LOAI_HANG Where TEN_LOAI = @Code ";
                cmd.CommandText = strSQL;
                Console.WriteLine(strSQL);
                cmd.Parameters.AddWithValue("@Code", code);
                result = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                return 0;
            }
            finally
            {
                provider.Disconnect();
            }
            return result;
        }		

        public void Insert(ref String errConnect, ref String errSQL, loaihanginfo info){
            cmd.Connection = provider.Connection(ref errConnect);
		    if(errConnect != "")
            {
			    return;
		    }
		    cmd.Transaction = provider.BeginTransaction();
		    try{	
			    SqlParameter f = new SqlParameter();
			    f.IsNullable = true;
                f.Value = System.DBNull.Value;
			    strSQL = "Insert into LOAI_HANG";
			    strSQL = strSQL + "(TEN_LOAI)";		 
			    strSQL = strSQL + " values";
			    strSQL = strSQL + "(@TEN_LOAI)";		 
			    cmd.CommandText = strSQL;
			    cmd.Parameters.Clear();
			    cmd.Parameters.AddWithValue("@TEN_LOAI",info.TEN_LOAI);
			    cmd.ExecuteNonQuery();
			    provider.CommitTransaction();
		    }
		    catch (Exception ex) 
		    {
			    errSQL = ex.Message;
			    provider.RollbackTransaction();
			    //' Neu xay ra loi thi rollback lai
		    }
		    finally 
		    {
			    //'Dong ket noi
			    cmd.Connection.Close();
			    cmd.Connection.Dispose();
		    }
    		
	    }

        public void Update(ref String errConnect, ref String errSQL, loaihanginfo info){
                cmd.Connection = provider.Connection(ref errConnect);
                if (errConnect != "")
                {
                    return;
                }
		    cmd.Transaction = provider.BeginTransaction();
		    try{
                strSQL = "Update LOAI_HANG SET ";
                //Console.WriteLine(strSQL);
                strSQL = strSQL + "TEN_LOAI=@TEN_LOAI Where ID_LOAI = @ID_LOAI";
                cmd.CommandText = strSQL;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ID_LOAI", info.ID_LOAI);
                cmd.Parameters.AddWithValue("@TEN_LOAI", info.TEN_LOAI);           
			    cmd.ExecuteNonQuery();
                provider.CommitTransaction();
                
		    }
		    catch (Exception ex) 
		    {
		        //errSQL = ex.Message;
                Library.ShowMessage(ex.Message);
                provider.RollbackTransaction();
		    }
		    finally 
		    {
			    cmd.Connection.Close();
			    cmd.Connection.Dispose();
		    }
    		
	    }

        public void Delete(ref String errConnect, ref String errSQL,int id ){
		    cmd.Connection = provider.Connection(ref errConnect);
		    if(errConnect != ""){
			    return;
		    }
		    cmd.Transaction = provider.BeginTransaction();
		    try {
			    strSQL = "DELETE FROM LOAI_HANG WHERE ID_LOAI=@RowID";
			    cmd.Parameters.Add("@RowID", SqlDbType.Int).Value = id;
			    cmd.CommandText = strSQL;
			    cmd.ExecuteNonQuery();
			    provider.CommitTransaction();
			    //' Thuc hien transaction 
			    } 
		    catch (Exception ex) {
				    errSQL = ex.Message;
				    provider.RollbackTransaction();
			    //' Neu xay ra loi thi rollback lai
			    }
		    finally {
			    //'Dong ket noi
				    cmd.Connection.Close();
				    cmd.Connection.Dispose();
			    }
	    }

        
    }


}